
using UnityEditor;
using UnityEngine;
using HotUpdate.Core.Editor;
using System;
using GameFramework;
namespace HotUpdate.Editor {
    [CustomPropertyDrawer(typeof(SerializableObjectAttribute))]
    public class SerializableObjectAttributeDrawer : PropertyDrawer
    {        
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            SerializableObjectAttribute attr = attribute as SerializableObjectAttribute;
            var m_TypeNames = HotUpdate.Core.Editor.Type.GetRuntimeTypeNames(attr.SerializableType);
            int nameIndex = -1;
            if (property.managedReferenceValue != null)
            {
                for (int i = 0; i < m_TypeNames.Length; i++)
                {
                    if (m_TypeNames[i] == property.managedReferenceValue.GetType().FullName)
                    {
                        nameIndex = i;
                        break;
                    }
                }
            }
            var p1 = new Rect(position);
            p1.height = EditorGUIUtility.singleLineHeight;
            int selectedIndex = EditorGUI.Popup(p1, "可选类型", nameIndex, m_TypeNames);
            if (selectedIndex != nameIndex)
            {
                var type = m_TypeNames[selectedIndex];
                property.managedReferenceValue = Activator.CreateInstance(Utility.Assembly.GetType(type));
            }
            if (selectedIndex == -1)
            {
                property.managedReferenceValue = null;
            }
            //绘制属性
            var p2 = new Rect(position);
            p2.height -= EditorGUIUtility.singleLineHeight;
            p2.y += EditorGUIUtility.singleLineHeight;
            EditorGUI.PropertyField(p2, property, new GUIContent("属性"), true);

        }
        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            return EditorGUIUtility.singleLineHeight + EditorGUI.GetPropertyHeight(property) ;
        }
    }

}

